﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils.DC.Web.UI;

public partial class Admin_Form_ProductCategory_Add : System.Web.UI.Page
{
    FunctionPageUI objFunctionPage = new FunctionPageUI();
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    DataSet dsFunctionPage;
    DataSet dsCategoryPage;
    int productPage = 4;

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetByParent(productPage);
        dsFunctionPage = objFunctionPage.GetAll();
        if (!IsPostBack)
        {
            BindCategoryPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtFunctionPageName.Text.Trim());

        //Gen url path cua trang moi tao
        string urlPath = General.GenUrlPath((cboCategoryPage.SelectedValue.Trim() == "" ? productPage : Convert.ToInt32(cboCategoryPage.SelectedValue)), alias, dsFunctionPage);

        string controlPath = "/Module/Product/san_pham_function.ascx";

        if (txtFunctionPageID.Value.Trim() == "")
        {
            try
            {
                int result = objCategoryPage.Add(txtFunctionPageName.Text, alias, txtFunctionPageTitle.Text, txtFunctionPageImage.Text, "",
                    txtMetaKeyword.Text, txtMetaDescription.Text, true, true, Convert.ToInt32(txtViewOrder.Text),
                    false, 0, controlPath, urlPath,
                    txtFunctionPageDescription.Text, "", Convert.ToInt32(cboCategoryPage.SelectedValue), false, false, false);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objCategoryPage.Update(Convert.ToInt32(txtFunctionPageID.Value), txtFunctionPageName.Text, alias, txtFunctionPageTitle.Text, txtFunctionPageImage.Text, "",
                    txtMetaKeyword.Text, txtMetaDescription.Text, true, true, Convert.ToInt32(txtViewOrder.Text),
                    false, 0, controlPath, urlPath,
                    txtFunctionPageDescription.Text, "", (cboCategoryPage.SelectedValue.Trim() == "" ? productPage : Convert.ToInt32(cboCategoryPage.SelectedValue)), false, false, false);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ReInsertData();
    }

    #region Cac ham tuong tac du lieu

    private void BindCategoryPage()
    {
        try
        {
            cboCategoryPage.DataSource = dsCategoryPage.Tables[0];
            cboCategoryPage.DataBind();
        }
        catch (Exception e)
        {

        }
    }

    private void LoadEditData(int CategoryPageID)
    {
        DataSet ds = objCategoryPage.GetByID(CategoryPageID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtFunctionPageID.Value = row["FunctionPageID"].ToString();
            txtFunctionPageDescription.Text = row["CategoryPageAbstract"].ToString();
            txtFunctionPageImage.Text = row["FunctionPageImage"].ToString();
            txtFunctionPageName.Text = row["FunctionPageName"].ToString();
            txtFunctionPageTitle.Text = row["FunctionPageTitle"].ToString();
            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            General.SetSelectedItemInComboBox(row["ParentID"].ToString(), cboCategoryPage);
            btnCreate.Text = "Cập nhật";
        }
    }

    private void ReInsertData()
    {
        //txtFunctionPageID.Value = "";
        txtFunctionPageDescription.Text = "";
        txtFunctionPageImage.Text = "";
        txtFunctionPageName.Text = "";
        txtFunctionPageTitle.Text = "";
        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtViewOrder.Text = "1";
        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #endregion
}
